<?php

namespace XWAM\Addon\POrgCustomer\Controller;

/**
 * CustomerController 类
 *
 * @since 1.0 <2015-12-5> SoChishun <14507247@qq.com> Added.
 */
class CustomerDispatchController extends AddonbaseController {

    /**
     * 获取客户列表搜索数据
     * @return array
     * @since 1.0 2014-12-30 by sutroon
     */
    function customer_list_get_search() {
        $search = $_GET;
        if (!empty($search['telphone'])) {
            $where['telphone'] = array('like', '%' . $search['tel']);
        }
        if (!empty($search['name'])) {
            $where['name'] = array('like', '%' . $search['name'] . '%');
        }
        if (!empty($search['serial_no'])) {
            $where['serial_no'] = $search['serial_no'];
        }
        if (!empty($search['buy_count'])) {
            $where['buy_count'] = array('GT', $search['buy_count']);
        }
        if (!empty($search['total_amount'])) {
            $where['total_amount'] = array('GT', $search['total_amount']);
        }
        if ($this->user_login_data['type_name'] == 'SEAT') {
            $where['user_name'] = $this->user_login_data['user_name'];
        }
        return array('search' => $search, 'where' => $where);
    }

    // 客户列表
    public function customer_list() {
        $asearch_info = $this->customer_list_get_search();
        if (I('action') == 'export') {
            $list = AddonD('Customer')->field("name as '客户姓名', telphone as '电话号码', address as '联系地址', serial_no as '客户编号', user_name as '工号'")->where($asearch_info['where'])->select();
            if (!$list) {
                $this->error('找不到符合条件的记录!');
            }
            sofn_excel_export($list);
        } else {
            $list = D('Common')->get_paging_list($asearch_info['where'], $show, array('ORDERBY' => 'id desc', 'PAGEPARAMS' => $asearch_info['search'], 'TABLE' => 't_porg_customer'));
            $this->assign('page', $show);
            $this->assign('list', $list);
            $this->assign('search', $asearch_info['search']);
            $this->display_cpp();
        }
    }

    // 客户调度记录 2014-12-25 by sutroon
    public function dispatch_log_list($customer_id) {
        if (!$customer_id) {
            $this->dialogClose(false, '参数有误!');
        }
        $list = M('v_porg_customer_appointment')->where("customer_id=$customer_id")->select();
        $this->assign('list', $list);
        $this->display_cpp();
    }

    // 获取客户调度查询表单数据 2014-12-25 by sutroon
    function dispatch_edit_get_search() {
        $search['lastBuyTimeExpBegin'] = I('lastBuyTimeExpBegin');
        $search['lastBuyTimeBegin'] = I('lastBuyTimeBegin');
        $search['lastBuyTimeExpEnd'] = I('lastBuyTimeExpEnd');
        $search['lastBuyTimeEnd'] = I('lastBuyTimeEnd');
        $search['lastVisitTimeExpBegin'] = I('lastVisitTimeExpBegin');
        $search['lastVisitTimeBegin'] = I('lastVisitTimeBegin');
        $search['lastVisitTimeExpEnd'] = I('lastVisitTimeExpEnd');
        $search['lastVisitTimeEnd'] = I('lastVisitTimeEnd');
        $search['buyCountExp'] = I('buyCountExp');
        $search['buyCount'] = I('buyCount');
        $search['cumulativeAmountExp'] = I('cumulativeAmountExp');
        $search['cumulativeAmount'] = I('cumulativeAmount');
        $search['oldUsers'] = I('oldUsers');
        $search['buyCountState'] = I('buyCountState');
        $search['searchNumber'] = I('searchNumber');
        if ($search['lastBuyTimeExpBegin'] && $search['lastBuyTimeBegin']) {
            $where['lastBuyTime'] = array($search['lastBuyTimeExpBegin'], $search['lastBuyTimeBegin']);
        }
        if ($search['lastBuyTimeExpEnd'] && $search['lastBuyTimeEnd']) {
            if (isset($where['lastBuyTime'])) {
                $where['lastBuyTime'] = array($where['lastBuyTime'], array($search['lastBuyTimeExpEnd'], $search['lastBuyTimeEnd']));
            } else {
                $where['lastBuyTime'] = array($search['lastBuyTimeExpEnd'], $search['lastBuyTimeEnd']);
            }
        }

        if ($search['lastVisitTimeExpBegin'] && $search['lastVisitTimeBegin']) {
            $where['lastVisitTime'] = array($search['lastVisitTimeExpBegin'], $search['lastVisitTimeBegin']);
        }
        if ($search['lastVisitTimeExpEnd'] && $search['lastVisitTimeEnd']) {
            if (isset($where['lastVisitTime'])) {
                $where['lastVisitTime'] = array($where['lastVisitTime'], array($search['lastVisitTimeExpEnd'], $search['lastVisitTimeEnd']));
            } else {
                $where['lastVisitTime'] = array($search['lastVisitTimeExpEnd'], $search['lastVisitTimeEnd']);
            }
        }
        if ($search['buyCount'] && $search['buyCountExp']) {
            $where['buyCount'] = array($search['buyCountExp'], $search['buyCount']);
        }
        if (strlen($search['buyCountState']) > 0) {
            switch ($search['buyCountState']) {
                case '1':
                    $where['buyCount'] = array('GT', 0);
                    break;
                case '0':
                    $where['buyCount'] = array('EQ', 0);
                    break;
                case '2':
                    if ($where['buyCount']) {
                        unset($where['buyCount']);
                    }
                    break;
            }
        }
        if ($search['cumulativeAmount'] && $search['cumulativeAmountExp']) {
            $where['cumulativeAmount'] = array($search['cumulativeAmountExp'], $search['cumulativeAmount']);
        }
        if ($search['oldUsers']) {
            $search['oldUsers'] = str_replace("'", '', $search['oldUsers']);
            $search['oldUsers'] = str_replace(' ', '', $search['oldUsers']);
            $search['oldUsers'] = str_replace(",", "','", $search['oldUsers']);
            $uids = M('tuser')->where("userType='SEAT' and userName in ('" . $search['oldUsers'] . "')")->getField('id', true);
            if ($uids) {
                $where['userID'] = array('IN', $uids);
            }
        }
        return array('search' => $search, 'where' => $where);
    }

    // 客户调度 2014-12-25 by sutroon
    public function dispatch_full_edit($customer_id = 0) {
        if ($customer_id) {
            $search = false;
            $where['id'] = $customer_id;
        } else {
            $arr = $this->dispatch_edit_get_search();
            $search = $arr['search'];
            $where = $arr['where'];
        }
        $count = 0;
        $hasOrderCount = 0;
        if ($where) {
            $list = AddonD('Customer')->where($where)->field('id, name, buy_count, user_name')->select();
            if ($list) {
                $count = count($list);
                foreach ($list as $row) {
                    if ($row['buyCount'] > 0) {
                        $hasOrderCount++;
                    }
                }
            }
        }
        $this->assign('count', $count);
        $this->assign('hasOrderCount', $hasOrderCount);
        $this->assign('list', $list);
        $this->assign('search', $search);
        $this->display();
    }

    // 客户调度 2016-2-2 by sutroon
    public function dispatch_edit($customer_id = '') {
        if (!$customer_id) {
            $this->dialogClose(false, '参数无效!');
        }
        $count = 0;
        $has_order_count = 0;
        $where = array('id' => array('in', $customer_id));
        $list = AddonD('Customer')->where($where)->field('id, serial_no, name, buy_count, user_name')->select();
        if ($list) {
            $count = count($list);
            foreach ($list as $row) {
                if ($row['buy_count'] > 0) {
                    $has_order_count++;
                }
            }
        }
        $this->assign('sum',array('count'=>$count,'has_order_count'=>$has_order_count));
        $this->assign('list', $list);
        $this->display();
    }

    // 客户调度数据保存 2014-12-25 by sutroon
    public function dispatch_edit_save() {
        $result=AddonD('CustomerDispatch')->save_dispatch();
        $this->dialogJump($result['status'],$result['info']);
    }

}
